package com.ulook.lookplanet.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.WidgetData;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TSysMsg;
import com.ulook.lookplanet.ui.notifications.VMsgBase;
import com.ulook.lookplanet.ui.notifications.VMsgSys;
import com.ulook.lookplanet.ui.notifications.VMsgUser;
import com.ulook.lookplanet.ui.notifications.chat.ChatActivity;
import com.ulook.lookplanet.ui.notifications.chat.SyschatActivity;
import com.ulook.lookplanet.util.StringUtil;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

import org.litepal.LitePal;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/29
 */
public class MsgView extends LinearLayout implements WidgetData<VMsgBase> {

    private Context context;
    private VMsgBase vMsgBase;
    private int currUserId;
    private int fromUserId;

    private TextView content;
    private QMUIRoundButton point;
    private QMUIRadiusImageView2 head;
    private TextView name;

    public MsgView(Context context) {
        super(context);
        initNotifView(context);
    }

    public MsgView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initNotifView(context);
    }

    public MsgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initNotifView(context);
    }

    private void initNotifView(Context context){
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.widget_msg_view, this, true);
    }

    @Override
    public void setWidgetData(VMsgBase msgBase) {
        head = findViewById(R.id.w_msg_head_img);
        name = findViewById(R.id.w_msg_name_tx);
        content = findViewById(R.id.w_msg_content_tx);
        point = findViewById(R.id.w_msg_point);

        if(msgBase instanceof VMsgUser){
            // 用户消息
            VMsgUser vMsgUser = (VMsgUser) msgBase;
            fromUserId = vMsgUser.getFromuser();
            name.setText(vMsgUser.getFromname());
            content.setText(vMsgUser.getMsg());
            if(!StringUtil.isEmpty(vMsgUser.getFromheadimg())){
                ResponseCallback callback = new ResponseCallback() {
                    @Override
                    public void onSuccess(Object res) {
                        byte[] image = (byte[]) res;
                        Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                        head.setImageBitmap(bitmap);
                    }
                    @Override
                    public void onFailure(OkHttpException failuer) {
                        ThrowException.print(failuer, failuer.getEmsg());
                    }
                };
                AsynRequestUtil.loadImg(AppServer.HEAD_IMG_PATH + vMsgUser.getFromheadimg(), null, callback);
            }
            // 点击跳转通知页面，用户
            this.setOnClickListener(view -> {
                // 清除红点
                setPoint(0);
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra("VMsgUser", vMsgUser);
                //Log.d("#####-MsgView跳转", vMsgUser.toString());
                context.startActivity(intent);
            });
        }else if(msgBase instanceof VMsgSys){
            // 系统消息
            List<TSysMsg> unreadList = LitePal.where("userid = ? and state = 0", currUserId+"").order("sendtime").find(TSysMsg.class);
            List<TSysMsg> sysMsgs = LitePal.order("sendtime desc").limit(1).find(TSysMsg.class);
            if(sysMsgs != null && sysMsgs.size() > 0){
                Log.d("#####", sysMsgs.get(0).toString());
                head.setImageResource(R.mipmap.lookplanet);
                name.setText(R.string.msg_sys_name);
                content.setText(sysMsgs.get(0).getMsg());
                point.setVisibility(VISIBLE);
                setPoint(unreadList.size());
                this.setClickable(true);
                // 点击跳转通知页面，系统
                this.setOnClickListener(view -> {
                    // 清除红点
                    setPoint(0);
                    Intent intent = new Intent(context, SyschatActivity.class);
                    intent.putExtra("currUserId", currUserId);
                    context.startActivity(intent);
                });
            }else {
                this.setVisibility(GONE);
            }

        }
    }

    @Override
    public void setWidgetData(VMsgBase msgBase, int currUserID) {
        this.currUserId = currUserID;
        setWidgetData(msgBase);
    }

    public void setMsgContent(String msg){
        content.setText(msg);
    }

    public void setPoint(int num){
        if(num == 0){
            point.setText("0");
            point.setVisibility(INVISIBLE);
        }else {
            point.setVisibility(VISIBLE);
            point.setText(num+"");
        }
    }

    public int getPoint(){
        return Integer.parseInt(point.getText().toString());
    }

    public int getFromUserId(){
        return fromUserId;
    }
}
